define(["angularAMD"], function (angularAMD) {
    angularAMD.service('LoadTemplateService', ['$state','$rootScope', '$filter', '$stateParams', 'config', '$mdDialog', 'MMWService',
        function ($state, $rootScope, $filter, $stateParams, config, $mdDialog, $MMWService) {

            var service = this;

            // model_no : 1(正向追溯)、2(生產追溯)
            service.init = function(model_no, callbackDashboardPanel){
                var content = {
                    model_no: model_no
                };
                userTemplateInfoGet(content,function(data){
                    var mock_user_template = [
                        {"model_name":"123","mode_sheets":[{"mode_sheet":"plotno_details","seq":2},{"mode_sheet":"plotno_his_details","seq":1},{"mode_sheet":"workerlogin_details","seq":3},{"mode_sheet":"workreport_details","seq":4},{"mode_sheet":"materialopt_details","seq":5},{"mode_sheet":"accessoryopt_details","seq":6},{"mode_sheet":"trade_details","seq":7},{"mode_sheet":"process_details","seq":8}]}
                    ]
                    data.user_template = mock_user_template;
                    openDialog(model_no, data.user_template, callbackDashboardPanel);
                });
            };

            function openDialog(model_no, user_template, callbackDashboardPanel){
                $mdDialog.dialog(
                    'module/moduleQT/program/service/LoadTemplateService.html',
                    function (dialog) {
                        return {
                            css_url:'module/moduleQT/program/service/LoadTemplateService.css',
                            model_no: model_no,
                            user_template: user_template,
                            back:function(){
                                dialog.hide();
                            },
                            getDashboardPanel: function(item){
                                callbackDashboardPanel(item.model_setting);
                                dialog.hide();
                            },
                            // todo: 刪除服務待修
                            deleteMode: function(item){
                                return
                                var confirmDialogHandler = function (confirmDialog) {
                                    var content = {
                                        model_no : dialog.model_no,
                                        model_name: item.model_name
                                    };
                                    var callback = function(data) {
                                        if(data.code == '1'){
                                            $rootScope.showToast($filter('translate')('QT_standard.common.loadModule.msg.delete_success'));
                                            dialog.user_template.splice(dialog.user_template.indexOf(item), 1);
                                            confirmDialog.hide();
                                        } else {
                                            $rootScope.showAlert($filter('translate')('QT_standard.common.loadModule.msg.delete_fail'));
                                        };  
                                    }
                                    userTemplateProcess(content, callback);
                                }
                                $rootScope.showConfirm($filter('translate')('QT_standard.common.loadModule.msg.whether_delete_this_module'), confirmDialogHandler); 
                            },
                            selectMode: function(itemIndex) {
                                dialog.user_template.forEach(function(elem, index){
                                    if (index == itemIndex) {
                                        elem.isSelect = !elem.isSelect;
                                        return
                                    }
                                    elem.isSelect = false;
                                })
                            },
                            isDisabled: function() {
                                return $filter('filter')(dialog.user_template, {isSelect: true}).length <= 0;
                            },
                            confirm: function() {
                                if (dialog.isDisabled()) return
                                var setting = $filter('filter')(dialog.user_template, {isSelect: true})[0];
                                callbackDashboardPanel(setting);
                                dialog.hide();
                            },
                        };
                    }
                );
            };

            // 取得模板資料
            function userTemplateInfoGet(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_LEAN.user_template_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 模板更新服務: 新增，檢查，調整排序
            function userTemplateProcess(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_LEAN.user_template_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };
        }
    ])
});
